//
//  ZPBAllViewController.m
//  WZLProduct
//
//  Created by boys on 16/3/26.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "ZPBAllViewController.h"
#import "ZPBAllCollectionViewCell.h"
#import "ZPBChildModel.h"
#import "ZPBAllCollectionReusableView.h"
#import "ZPBAllModel.h"
#import "SongListViewController.h"

@interface ZPBAllViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *sectionArrData; ///保存分区数组
@property (nonatomic, strong) UICollectionView *collectionView; ///collectionView

@property (nonatomic, strong) NSMutableArray *dataSoucse;     ///源数据数组

@property (nonatomic, strong) UIButton *button;///全部按钮

@end

@implementation ZPBAllViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.sectionArrData = [NSMutableArray array];
        self.dataSoucse = [NSMutableArray array];
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.navigationItem.title = @"分类";
    self.view.backgroundColor  = [UIColor whiteColor];
    [self createViews];
    [self dataHandle];
}
#pragma mark - 创建UICollectionView
- (void)createViews {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 70, kScreenWidth, kScreenHeight - 164) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_collectionView];
    
    //注册
    [_collectionView registerClass:[ZPBAllCollectionViewCell class] forCellWithReuseIdentifier:@"ZPBAllCollectionViewCell"];
    [_collectionView registerClass:[ZPBAllCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZPBAllCollectionReusableView"];
    
    //创建头部button
    [self.view addSubview:_collectionView];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(5, 10, kScreenWidth - 10, 50);
    _button.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [_button setTitle:@"全部" forState:UIControlStateNormal];
    
    [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _button.titleLabel.font = FONT16;
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
    
}

#pragma mark - UICollectionView Delegate & DataSource
// 每个分区item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((kScreenWidth - 25) / 4, 50);
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    ZPBAllModel *model = _dataSoucse[section];
    
    return model.childrenArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZPBAllCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBAllCollectionViewCell" forIndexPath:indexPath];
    ZPBAllModel *model = _dataSoucse[indexPath.section];
    ZPBChildModel *smallModel = model.childrenArr[indexPath.row];
    
    cell.model = smallModel;
    
    
    return cell;
    
}

#pragma mark collectionView的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZPBAllModel *model = _dataSoucse[indexPath.section];
    ZPBChildModel *smallModel = model.childrenArr[indexPath.row];
    
    self.block(smallModel.tag);
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return _dataSoucse.count;
    
}
#pragma mark 分区头赋值
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        
        ZPBAllCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ZPBAllCollectionReusableView" forIndexPath:indexPath];
        
        ZPBAllModel *model = _dataSoucse[indexPath.section];
        header.nameString = model.tag;
        
        
        return header;
    }
    return nil;
}
#pragma mark header大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    
    return CGSizeMake(kScreenWidth, 40);
    
    
}
#pragma mark - 数据处理
- (void)dataHandle {
    
    NSString *path = [NSString stringWithFormat:@"http://api.dongting.com/song/tag/songlist?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude="];
    [RequestData GETRequestWithURLString:path completionWithSuccess:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            ZPBAllModel *model = [[ZPBAllModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSoucse addObject:model];
            
            
            [_collectionView reloadData];
        }
    } WithFailure:^(id error) {
        NSLog(@"%@", error);
    }];
    
}

//全部点击方法
- (void)buttonAction:(UIButton *)button {
    
    SongListViewController *vc = (SongListViewController *)self.navigationController.viewControllers[2];
    
    [self.delegate withNSString:@"最热"];

    [self.navigationController popToViewController:vc animated:YES];
    
}



                
- (void)didReceiveMemoryWarning {
                    [super didReceiveMemoryWarning];
                    // Dispose of any resources that can be recreated.
}
                
    /*
     #pragma mark - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     // Get the new view controller using [segue destinationViewController].
     // Pass the selected object to the new view controller.
     }
     */
                
                @end
